<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/22/022
 * Time: 18:57
 */

namespace app\admin\controller;


use app\Lib\Api\Menu\getMenuAllMenu;
use app\Lib\Api\Role\addRole;
use app\Lib\Api\Role\editRole;
use app\Lib\Api\Role\getRoleInfo;
use app\Lib\Api\Role\getRoleList;
use app\Lib\Api\Role\getRolePage;
use app\Lib\Api\Role\getRoleSelectList;
use app\Lib\TopClient;
use think\Url;

class Role extends Base
{
    public function  index(){
        $this->assign('ajaxList',Url::build('admin/Role/ajaxList'));
        $this->assign('ajaxPage',Url::build('admin/Role/ajaxPage'));
        $this->assign('info','/admin/Role/roleInfo');
        return $this->fetch();
    }


    public function ajaxList(){
        try{
            $page = input('page');
            $limit = input('limit');
            $model = new getRoleList();
            $model->setArgs('page',$page);
            $model->setArgs('limit',$limit);
            $result = (new TopClient($model))->getSuccessData();
            $this->assign('result',$result);
            return $this->fetch('role/ajaxList');
        }catch (\Exception $e){
            echo  $e->getMessage();
        }
    }


    public function ajaxPage(){
        try{
            $model =  new getRolePage();
            $result = (new TopClient($model))->getSuccessData();
            $this->assign('result',$result);
            return $result;
        }catch (\Exception $e){
            echo  $e->getMessage();
        }
    }

    /**
     * @return mixed
     */
    public function roleInfo(){
        try{
           if($this->request->isPost()){
            $post = input('post.');
            if($post['id']>0){
                $this->editRole($post);
            }else{
                $this->addRole($post);
            }
               return ['code'=>1,'msg'=>'操作成功'];
           }else{
               $role = input('menu_id',0);
               $info = $this->getRoleInfo($role);
               $authMenu = $this->getMenuList();
               $allRole = $this->getRoleSelect();
//               dump($allRole);
               $this->assign('addUrl',Url::build('admin/Role/roleInfo'));
               $this->assign('info',$info);
               $this->assign('authMenu',$authMenu);
               $this->assign('allRole',$allRole);
               return $this->fetch();
           }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }

    /**
     * @param $role
     * @return array|string
     * @throws \Exception
     */
    private function getRoleInfo($role){
        if($role == 0){
            $data = [
                'id'=>0,
                'name'=>'',
                'parent_id'=>0,
                'role_alias'=>'',
                'auth'=>[],
                'is_admin_login'=>true
            ];
            return $data;
        }
//        dump($role);
        $info = (new getRoleInfo());
        $info->setArgs('roleId',$role);
        $result = (new TopClient($info))->getSuccessData();
        $auth = $result['auth'];
        if(!empty($auth)){
            $auth = json_decode($auth);
            $newAuth = [];
            foreach ($auth as $v){
                $newAuth[$v] = $v;
            }
            $result['auth']= $newAuth;
        }
//        dump($result);

        return $result;
    }

    /**
     * @param $post
     * @return bool
     * @throws \Exception
     */
    private function addRole($post){
    //‘engineeringDepartmentManager’
        $model = (new addRole());
        $model->setArgs('opUid',$this->login_uid);
        $model->setArgs('name',$post['name']);
        $model->setArgs('roleAlias',$post['role_alias']);
        $model->setArgs('parentId',$post['parentId']);
        $model->setArgs('auth',$post['authMenu']);
        $model->setArgs('is_admin_role',$post['is_admin_login']);
        $result = (new TopClient($model))->getSuccessData();
        return true;
    }

    /**
     * @param $post
     * @return bool
     * @throws \Exception
     */
    private function editRole($post){
        $model = (new editRole());
        $model->setArgs('opUid',$this->login_uid);
        $model->setArgs('name',$post['name']);
        $model->setArgs('roleAlias',$post['role_alias']);
        $model->setArgs('parentId',$post['parentId']);
        $model->setArgs('auth',empty($post['authMenu'])?[]:$post['authMenu']);
        $model->setArgs('roleId',$post['id']);
        $model->setArgs('is_admin_role',$post['is_admin_login']);
        $result = (new TopClient($model))->getSuccessData();
        return true;
    }


    /**
     * @return array|string
     * @throws \Exception
     */
    private function getMenuList(){
        $model =  new getMenuAllMenu();
        $topClient = new TopClient($model);
        $data = $topClient->getSuccessData();
        return $data;
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    private function getRoleSelect(){
        $model = new getRoleSelectList();
        $result =  (new TopClient($model))->getSuccessData();
        $result[0] = '顶级角色';
//        $result = array_merge($data,$result);
        return $result;
    }
}